package service;

import begin.UserMenu;
import dao.UserDao;
import tools.UtilData;
import tools.UtilMessage;

/**
 * @Author 王志康
 * @Date 2021/9/6 11:07
 * @Version 1.0.0
 */
public class UserService {
    /**
     * 电话更改逻辑
     *
     * @param phoneChanged 想要更改成的电话
     */
    public static void phoneInformationController(String phoneChanged) {
        if (phoneChanged.length() != UtilData.TELEPHONE_NUMBER_DIGITS) {
            new UtilMessage().messageShow("请输入正确的手机号");
            return;
        }
        String phone = UserMenu.user.getPhoneNumber();
        String number = UserMenu.user.getNumber();
        UserMenu.user.setPhoneNumber(phoneChanged);
        UserMenu.user.setNumber("");
        if (UserDao.verification(UserMenu.user)) {
            System.out.println(number);
            UserMenu.user.setNumber(number);
            UserDao.updateUser(UserMenu.user);
            new UtilMessage().messageShow("手机号更改成功");
        } else {
            UserMenu.user.setNumber(number);
            UserMenu.user.setPhoneNumber(phone);
            new UtilMessage().messageShow("此手机号已被使用");
        }
    }

    /**
     * 密码更改逻辑
     *
     * @param passwordChanged 想要更改成的密码
     */
    public static void passwordInformationController(String passwordChanged){
        if(passwordChanged.equals(UserMenu.user.getPassword())){
            new UtilMessage().messageShow("新密码不能与旧密码一致");
            return;
        }
        UserMenu.user.setPassword(passwordChanged);
        UserDao.updateUser(UserMenu.user);
        new UtilMessage().messageShow("密码更改成功");
    }
}
